package com.shop2cn.iapi.agentproducts.testcase;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import org.apache.http.client.ClientProtocolException;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.shop2cn.iapi.agentproducts.parameter.AddAgentProductBean;
import com.shop2cn.iapi.agentproducts.parameter.ImportAgentProductBean;
import com.shop2cn.iapi.agentproducts.parameter.SetOffshelfBean;
import com.shop2cn.iapi.agentproducts.service.SetOffshelfCall;
import com.shop2cn.iapi.agentproducts.service.SetOffshelfCallV2;
import com.ymatou.iapi.prodstock.parameter.AgentProductPo;
import com.ymatou.mq.admin.parameter.MessageListBean;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.AddAgentProductCallService;
import com.ymttest.business.service.MqQueryCallService;
import com.ymttest.database.sqlwapper.YmtProductsIWapper;
import com.ymttest.utils.AgentUsers;

/**
*下架
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_SetOffshelf{
	private static SetOffshelfBean setoffshelfBean;
	private static SetOffshelfCall setoffshelfCall;
	private static YmtProductsIWapper ymtProductsIWapper = new YmtProductsIWapper();
	//渠道商
	private static int rootUserId = AgentUsers.rootUserId;
	private static String rootUserName = "NickautoTest";
	//渠道商->代理商1
	private static int agentUserId1 = AgentUsers.agentUserId1;
	private static String agentUserName1 = "NickautoTest";
	//渠道商->代理商2
	private static int agentUserId2 = AgentUsers.agentUserId2;
	private static String agentUserName2 = "NickautoTest";

	//vip渠道
	private static int vipRootUserId = AgentUsers.vipRootUserId;
	private static String vipRootUserName = "NickautoTest";
	//vip渠道->代理
	private static int vipAgentUserId1 = AgentUsers.vipAgentUserId1;
	private static String vipAgentUserName1 = "dgsq408897016";
	//vip渠道->代理->代理
	private static int vipAgentUserId2 = AgentUsers.vipAgentUserId2;
	private static String vipAgentUserName2 = "NickautoTest";
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("下架");
	}

	@Before
	public void caseUp() {
		setoffshelfBean=new SetOffshelfBean();
		setoffshelfCall=new SetOffshelfCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(setoffshelfCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	public void verifyMongoCurMq(String pid,int count) throws ClientProtocolException, IOException, InterruptedException {
		Thread.sleep(5000);
		MessageListBean MessageListBean2 = MqQueryCallService.initAgentMongoCrud(pid);
		List<Map> joa2 = MqQueryCallService.GetMqMessage(MessageListBean2);
		Logger.debug(joa2);
		Logger.verifyEquals(count,joa2.size(),"验证发送两条消息到总线");
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SetOffshelf_01() {
		Logger.start(true,"自采商品，商品下架");
		try {
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(rootUserId,rootUserName,false);
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			setoffshelfBean.setProductId(productId);
			setoffshelfBean.setUserId(rootUserId);
			setoffshelfCall.setData(setoffshelfBean);
			setoffshelfCall.callService();			
			Logger.verifyEquals("200",setoffshelfCall.getString("code"),"验证code");
			verifyMongoCurMq(productId,2);
			AgentProductPo agentProductPo = ymtProductsIWapper.selectAgentProductByProductId(productId);
			//验证商品状态为下架状态
			Logger.verifyEquals(2,agentProductPo.getStatus(),"验证status");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SetOffshelf_04() {
		Logger.start(true,"代理商品，商品下架，没有权限");
		try {
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(rootUserId,rootUserName,true);
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			ImportAgentProductBean importAgentProductBean = AddAgentProductCallService.initImportAgentProduct(agentUserId1,agentUserName1,productId,rootUserId,false);
			String agentProductId = AddAgentProductCallService.ImportAgentProduct(importAgentProductBean);
			setoffshelfBean.setProductId(agentProductId);
			setoffshelfBean.setUserId(agentUserId1);
			setoffshelfCall.setData(setoffshelfBean);
			setoffshelfCall.callService();
			Logger.verifyEquals("201",setoffshelfCall.getString("code"),"验证code");
			AgentProductPo agentProductPo = ymtProductsIWapper.selectAgentProductByProductId(productId);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SetOffshelf_05() {
		Logger.start(true,"vip渠道->vip代理->vip代理，vip渠道商品下架，下级vip代理商品状态不变");
		try {
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(vipRootUserId,vipRootUserName,true);
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			ImportAgentProductBean importAgentProductBean = AddAgentProductCallService.initImportAgentProduct(vipAgentUserId1,vipAgentUserName1,productId,vipRootUserId,true);
			String agentProductId = AddAgentProductCallService.ImportAgentProduct(importAgentProductBean);
			ImportAgentProductBean importAgentProductBean1 = AddAgentProductCallService.initImportAgentProduct(vipAgentUserId2,vipAgentUserName2,agentProductId,vipAgentUserId1,false);
			String agentProductId1 = AddAgentProductCallService.ImportAgentProduct(importAgentProductBean1);

			setoffshelfBean.setProductId(productId);
			setoffshelfBean.setUserId(vipRootUserId);
			setoffshelfCall.setData(setoffshelfBean);
			setoffshelfCall.callService();
			Logger.verifyEquals("200",setoffshelfCall.getString("code"),"验证code");
			verifyMongoCurMq(productId,2);
			AgentProductPo agentProductPo = ymtProductsIWapper.selectAgentProductByProductId(productId);
			//验证渠道商
			Logger.verifyEquals(2,agentProductPo.getStatus(),"验证status");
//			AgentProductPo agentProductPo = ymtProductsIWapper.selectAgentProductByProductId(productId);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SetOffshelf_02() {
		Logger.start(true,"下架商品，商品不存在");
		try {
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(vipRootUserId,vipRootUserName,true);
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			setoffshelfBean.setProductId(productId);
			setoffshelfBean.setUserId(11);
			setoffshelfCall.setData(setoffshelfBean);
			setoffshelfCall.callService();
			Logger.verifyEquals("201",setoffshelfCall.getString("code"),"验证code");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SetOffshelf_03() {
		Logger.start(true,"下架商品，用户不存在");
		try {
			setoffshelfBean.setProductId("111");
			setoffshelfBean.setUserId(rootUserId);
			setoffshelfCall.setData(setoffshelfBean);
			setoffshelfCall.callService();
			Logger.verifyEquals("201",setoffshelfCall.getString("code"),"验证code");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SetOffshelf_06() {
		Logger.start(true,"渠道商客服下架商品，templateId4，成功");
		try {
			int csid = 500023286;
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(vipRootUserId,vipRootUserName,false);
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);

			setoffshelfBean.setProductId(productId);
			setoffshelfBean.setUserId(csid);
			SetOffshelfCallV2 setOffshelfCallV2 = new SetOffshelfCallV2();
			setOffshelfCallV2.addHeader("ymt-pars","accesstoken=C73D22F7D46D76799A6A0A7DFE5EABBBFADB332C40F560517F6D23115912D2A9D66752A159D46C5EF288EA2ACD7155EE0F7923987B3F24F5");

			setOffshelfCallV2.setData(setoffshelfBean);
			setOffshelfCallV2.callService();
			AgentProductPo agentProductPo = ymtProductsIWapper.selectAgentProductByProductId(productId);
			//验证商品状态为下架状态
			Logger.verifyEquals(2,agentProductPo.getStatus(),"验证status");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SetOffshelf_07() {
		Logger.start(true,"渠道商客服下架商品，templateId3，成功");
		try {
			int csid = 500023287;
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(vipRootUserId,vipRootUserName,false);
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);

			setoffshelfBean.setProductId(productId);
			setoffshelfBean.setUserId(csid);
			SetOffshelfCallV2 setOffshelfCallV2 = new SetOffshelfCallV2();
			setOffshelfCallV2.addHeader("ymt-pars","accesstoken=C73D22F7D46D7679B8B5287DA8F6C358B032C5D4BF69995D777F8750D5FABCF6A03EBC4B67F0B3347F243B92D24DC8EAB34A7E1D0816E1CA");

			setOffshelfCallV2.setData(setoffshelfBean);
			setOffshelfCallV2.callService();
			AgentProductPo agentProductPo = ymtProductsIWapper.selectAgentProductByProductId(productId);
			//验证商品状态为下架状态
			Logger.verifyEquals(2,agentProductPo.getStatus(),"验证status");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SetOffshelf_08() {
		Logger.start(true,"渠道商客服下架商品，templateId2，失败");
		try {
			int csid = 500023288;
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(vipRootUserId,vipRootUserName,false);
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);

			setoffshelfBean.setProductId(productId);
			setoffshelfBean.setUserId(csid);
			SetOffshelfCallV2 setOffshelfCallV2 = new SetOffshelfCallV2();
			setOffshelfCallV2.addHeader("ymt-pars","accesstoken=C73D22F7D46D767954A3F01B09D21A20479176177E62025B342BD096EA5F576089601F8445F4D7D4D699DCA8A26A1614D518EDA2019F0532");

			setOffshelfCallV2.setData(setoffshelfBean);
			setOffshelfCallV2.callService();
			Logger.verifyEquals("201",setOffshelfCallV2.getString("code"),"验证过code");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SetOffshelf_09() {
		Logger.start(true,"渠道商客服下架商品，templateId1，失败");
		try {
			int csid = 500023289;
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(vipRootUserId,vipRootUserName,false);
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);

			setoffshelfBean.setProductId(productId);
			setoffshelfBean.setUserId(csid);
			SetOffshelfCallV2 setOffshelfCallV2 = new SetOffshelfCallV2();
			setOffshelfCallV2.addHeader("ymt-pars","accesstoken=C73D22F7D46D767915F5F242E8D33853E112C14B8904A6AE559730333EF9D3573D244B59588F70E2BE7FDEE32326B219044BC1E17CF19372");

			setOffshelfCallV2.setData(setoffshelfBean);
			setOffshelfCallV2.callService();
			Logger.verifyEquals("201",setOffshelfCallV2.getString("code"),"验证过code");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

}